package IO;

import java.io.*;

public class SerializationTest {
    public static void main(String[] args) throws IOException {
        User user=new User("张三", 18);
        String filePath="user.ser";


        try {
            OutputStream out=new FileOutputStream(filePath);
            ObjectOutputStream oos=new ObjectOutputStream(out);
            oos.writeObject(user);
            System.out.println("序列化成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try{
            InputStream in=new FileInputStream(filePath);
            ObjectInputStream ois=new ObjectInputStream(in);
            User deserializedUser=(User)ois.readObject();
            System.out.println(deserializedUser.getName()+deserializedUser.getAge());
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
